package com.vms.control.weight;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import com.vms.LaunchActivityArgs;
import com.vms.NetLog;
import com.vms.Settings.ValueType;
import com.vms.control.weight.model.WeightModel;
import com.vms.control.weight.ui.DayChangeUi;

public class InputActivity extends ActionBarActivity {

	public static final String LAUNCH_ACTIVITY_ARGS = "LAUNCH_ACTIVITY_ARGS";
	private Button button1 = null;
	private Button button2 = null;

	private String someText = "some text here";
	
	protected WeightModel mModel;
	protected DayChangeUi mDayUi;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_activity);

		mModel = new WeightModel(getApplicationContext(),ValueType.WEIGHT_MORNING);
		Log.v(NetLog.TAG, "getExternalStorageDirectory:"
				+ Environment.getExternalStorageDirectory().getAbsolutePath());

		mDayUi = new DayChangeUi(this, mModel);
		loadViews();
		
	}

	private void loadViews() {
		initTestButtons();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.status:
	            Log.d("shdd", "Status menu");
	            InputActivity.this.startActivity(new Intent(InputActivity.this, StatusActivity.class));
	            return true;
	        case R.id.action_settings:
	        	Log.d("shdd", "Settings menu");
	        	InputActivity.this.startActivity(new Intent(InputActivity.this, SettingsActivity.class));
	            return true;
	        case R.id.input_keyb:
	        	Log.d("shdd", "Input_keyb menu");
//	        	InputActivity.this.startActivity(getInputActivityIntent(ValueType.WEIGHT_MORNING));
	        	InputActivity.this.startActivity(new Intent(getApplicationContext(), KeyboardInputActivity.class));
	            return true;
	        case R.id.action_grid:
	        	Log.d("shdd", "Grid activity");
	        	//InputActivity.this.startActivity(new Intent(InputActivity.this, GridActivity.class));
	        	PopupMenu popup = new PopupMenu(this, findViewById(R.id.action_grid));
	        	Menu menu = popup.getMenu();
	        	menu.add(Menu.NONE, 1, Menu.NONE, "one");
	        	menu.add(Menu.NONE, 2, Menu.NONE, "two");
	        	menu.addSubMenu(Menu.NONE, 11, Menu.NONE, "sub-one");
	        	menu.addSubMenu(Menu.NONE, 12, Menu.NONE, "sub-two");
	        	popup.show();
	            return true;
	        case R.id.action_toast_sample:
	        	sampleToast();
	        	return true;
	        case R.id.action_dialog_sample:
	        	sampleDialog();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

	private Intent getInputActivityIntent(ValueType type) {
		Intent intent = new Intent(InputActivity.this, InputSepActivity.class);
		intent.putExtra(LAUNCH_ACTIVITY_ARGS,new LaunchActivityArgs(type));
		return intent;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}


	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		Log.v(NetLog.TAG, "> onResume");
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onPause() {
		Log.v(NetLog.TAG, "> onPause");
		super.onPause();
	}


	private void initTestButtons() {
		this.button1 = (Button) findViewById(R.id.button1);
		this.button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				InputActivity.this.startActivity(getInputActivityIntent(ValueType.WEIGHT_MORNING));
			}
		});
		
		this.button2 = (Button)findViewById(R.id.button2);
		this.button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View button) {
				InputActivity.this.startActivity(getInputActivityIntent(ValueType.WEIGHT_EVENING));
			}
		});
	}
	
    protected void sampleToast() {
		View v =((LayoutInflater)InputActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
		                R.layout.toast, null);
		((TextView)v.findViewById(R.id.message)).setText("toast message here toast message here toast message toast message here");

		//((ImageView)v.findViewById(R.id.BilderToast)).setImageResource(resIcon);
		Toast mToast = new Toast(InputActivity.this);
		mToast.setView(v);
		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.show();
	}


	protected void sampleDialog() {
		Log.v(NetLog.TAG, String.format("WRITE to someText: %s", someText));
		Builder b = new AlertDialog.Builder(InputActivity.this);
		b.setTitle("title - title - title");
		b.setMessage("message --\n <b>bold<b/>. Look - it's raining...");
		b.setPositiveButton("да да да да", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Log.d("shdd","which="+which);
				dialog.dismiss();
			}
		});
		b.create().show();
	}
}
